package com.bw.service.impl;

import com.bw.domain.Role;
import com.bw.domain.TbMenu;
import com.bw.domain.User;
import com.bw.exception.MyRuntimeException;
import com.bw.mapper.UserMapper;
import com.bw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TaoShiHao
 * @Date: 2025/3/12 15:15
 * @Description:
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public User login(String username) {
        return userMapper.login(username);
    }

    @Override
    public List<Role> listRoleByUserId(Integer id) {
        return userMapper.listRoleByUserId(id);
    }

    @Override
    public List<TbMenu> listMenuByUserId(Integer id) {
        return userMapper.listMenuByUserId(id);
    }

    @Override
    public List<User> list(User user) {
        return userMapper.list(user);
    }

    @Override
    public List<Role> listRole() {
        return userMapper.listRole();
    }

    @Override
    public int checkName(String username) {
        return userMapper.checkName(username);
    }

    @Transactional(rollbackFor = MyRuntimeException.class)
    @Override
    public boolean add(User user) {
        try {
            userMapper.add(user);
            userMapper.addUserRole(user.getId(),user.getAuthId());
            return true;
        } catch (Exception e) {
            throw new MyRuntimeException("添加失败");
        }
    }
}
